// 用户相关api
// import * as utils from '@/utils'

/**
 * 根据token获取用户信息
 * @returns {*}
 */
export function getBookInfo (bookId) {
  return uni.flux.http.get('/external/book/v1/getBookInfo', {
    bookId: bookId || uni.flux.store.state.bookId,
    platform: uni.flux.store.state.env.isH5 ? 'h5' : 'weixin',
    equipment: uni.flux.store.state.env.isIos ? 'ios' : 'android'
  }).then(bookInfo => {
    uni.flux.store.commit('setBookInfo', bookInfo)
    return uni.flux.resolve(bookInfo)
  }).catch(err => {
    return uni.flux.reject(err)
  })
}

export function getAnswerDetails (param) {
  return uni.flux.http.post('/external/book/v1/getAnswerDetails', param)
}

/**
 * bookId: "64"
 * currentPage: 1
 * numPerPage: 20
 * pageNumber: ""
 * @returns {*}
 */
export function getQuestions (param) {
  return uni.flux.http.post('/external/book/v1/getScanCodeAnswerList', param)
}

export function getHomePageBanner (param) {
  return uni.flux.http.get('/external/configure/v1/getHomePageBanner').then(homeBanner => {
    uni.flux.store.commit('setHomeBanner', homeBanner)
  })
}

export function getScanCodeBanner () {
  return uni.flux.http.get('/external/configure/v1/getScanCodeBanner', {
    bookId: uni.flux.store.state.bookId
  }).then(scanCodeBanner => {
    uni.flux.store.commit('setScanCodeBanner', scanCodeBanner)
  })
}

/**
 * 历史记录
 * @param param
 * @returns {*|Promise<any>}
 */
export function getBookScanRecord () {
  return uni.flux.http.get('/external/book/v1/getBookScanRecord')
}

/**
 * 书籍题目
 * @param bookId
 * @param currentPage
 * @param numPerPage
 * @param pageNumber
 * @return {*|Promise<*|*|undefined>}
 */
export function getScanCodeAnswerList ({ bookId, currentPage, numPerPage, pageNumber }) {
  return uni.flux.http.post('/external/book/v1/getScanCodeAnswerList', {
    bookId,
    currentPage,
    numPerPage,
    pageNumber
  })
}

/**
 * 课程页：知识点精讲+联合卡
 * @param grade
 */
export function getReaderGrade () {
  return uni.flux.http.get('/external/h5/bookCard/v1/switchReaderGrade', {
    grade: uni.flux.store.state.userInfo.grade
  })
}
/**
 * 我的收藏
 * pageNumber: 1,
 * currentPage: 0,
 * numPerPage: 10,
 */
export function getFavorites ({ currentPage, numPerPage }) {
  return uni.flux.http.post('/external/reader/v1/getSubjectCollectForReader', { currentPage, numPerPage }, { load: true })
}

/**
 * 我的知识点精讲
 */
export function getMyAlbumList () {
  return uni.flux.http.get('/external/videoClass/v1/getMyAlbumList', '', { load: true })
}

/**
 * 我的畅读卡,
 */
export function getMyBookCardList () {
  return uni.flux.http.get('/external/h5/bookCard/v1/getMyBookCardList', '', { load: true })
}

export function getCourseConfirmInfo ({ bookId, cardId }) {
  return uni.flux.http.get('/external/h5/bookCard/v1/getCourseConfirmInfo', {
    bookId,
    cardId
  }, { load: true })
}

export function getCourseInfoByCardId ({ bookId, cardId }) {
  return uni.flux.http.get('/external/h5/bookCard/v1/getCourseInfoByCardId', {
    bookId,
    cardId
  })
}

export function getReaderHeadList () {
  return uni.flux.http.get('/external/reader/v1/getReaderHeadList')
}

/**
 * 地址
 *
 */
export function getShippingAddress () {
  return uni.flux.http.get('/external/h5/shippingAddress/v1/getShippingAddress')
}

/**
 *
 * @param shippingName
 * @param shippingPhone
 * @param shippingAddress
 * @return {*|Promise<*|{}|undefined>}
 */
export function addShippingAddress ({ shippingName, shippingPhone, shippingAddress }) {
  return uni.flux.http.post('/external/h5/shippingAddress/v1/addShippingAddress', {
    shippingName,
    shippingPhone,
    shippingAddress
  })
}

/**
 * 查询已购买的联合卡信息
 */
export function geyMyCardInfo () {
  return uni.flux.http.get('/external/h5/bookCard/v1/getMyBookCardList')
}

/**
 * 收藏题目
 * @returns {*}
 */
export function collectSubject ({ subjectId, collectStatus }) {
  return uni.flux.http.post('/external/subject/v1/collectSubject', { subjectId, collectStatus }, { load: true })
}

/**
 * 用户题目反馈新增
 * @param subjectId
 * @param collectStatus
 * @returns {*}
 */
export function addSubjectResponse ({ questionId, isHelpful, reason, reasons }) {
  return uni.flux.http.post('/external/subject/v1/addSubjectResponse', {
    questionId,
    isHelpful,
    reason,
    reasons
  })
}

// 获取礼物
export function getRewardGiftList () {
  return uni.flux.http.get('/external/subject/v1/getRewardGiftList')
}

/**
 * 联合卡支付成功获取支付成功订单信息
 * @param orderId
 * @return {*|Promise<*|{}|undefined>}
 */
export function getBookCardByOrderId ({ orderId }) {
  return uni.flux.http.get('/external/h5/bookCard/v1/getBookCardByOrderId', {
    orderId
  })
}

/**
 * 购买页推荐联合卡
 * @param bookId
 * @return {*|Promise<*|{}|undefined>}
 */
// export function getBookUnionCardList () {
//   return uni.flux.http.get('/external/h5/bookCard/v1/getBookUnionCardList', {
//     bookId: uni.flux.store.state.bookId
//   }).then((data) => {
//     uni.flux.store.commit('setQuizAd', { type: 'unionCardList', data })
//   })
// }

/**
 * 已购买畅读卡，点击领取资料
 * @param bookId
 * @return {*|Promise<*|{}|undefined>}
 */
export function getStudyResourceUrl ({ bookId }) {
  return uni.flux.http.get('/external/h5/bookCard/v1/getStudyResourceUrl', {
    bookId
  })
}

/**
 * 获取书籍章节
 * @param bookId
 * @return {*|Promise<*|*|undefined>}
 */
export function getBookChapterPage ({ bookId }) {
  return uni.flux.http.get('/external/book/v1/getBookChapterPage', {
    bookId: bookId || uni.flux.store.state.bookId
  })
}

/**
 * 保存书籍点选章节
 * @param bookId
 * @param pageNumber
 * @return {*|Promise<*|{}|undefined>}
 */
export function setLastPageNumber ({ bookId, pageNumber }) {
  return uni.flux.http.get('/external/book/v1/setLastPageNumber', {
    bookId,
    pageNumber
  })
}

/**
 * 首页扫码获取配置
 * @param bookId
 * @param pageNumber
 * @return {*|Promise<*|{}|undefined>}
 */
export function getHtmlPathByQrcode ({ qrcodeContent, qrcodeId }) {
  return uni.flux.http.get('/external/qrcode/v1/getHtmlPathByQrcode', {
    qrcodeContent,
    qrcodeId
  }, { load: true })
}

export function updateScanRecord (bookId) {
  return uni.flux.http.post('/external/reader/v1/updateScanRecord', {
    bookId: bookId || uni.flux.store.state.bookId,
    location: 'BODY_COPY',
    phoneModel: uni.flux.store.state.systemInfo.platform + uni.flux.store.state.systemInfo.model,
    qrcodeId: uni.flux.utils.getRandom(32)
  })
}

export function getBookScene (bookId) {
  return uni.flux.http.post('/external/book/v1/getBookScene', {
    bookId,
    platform: uni.flux.store.state.env.isH5 ? 'h5' : 'weixin',
    equipment: uni.flux.store.state.env.isIos ? 'ios' : 'android'
  })
}

// 畅读卡
export function getBookVipCard ({ bookId }) {
  return uni.flux.http.get('/external/h5/bookCard/v1/getBookVipCard', {
    bookId
  }).then(res => {
    if (res) {
      uni.flux.store.commit('setBookVipCard', res)
      uni.flux.resolve(res)
    }
  })
}

export function exportCollectSubject ({ subjectIdList, exportType }) {
  return uni.flux.http.post('/external/subject/v1/exportCollectSubject', {
    subjectIdList,
    exportType
  })
}
